package com.example.huazong.myapplication;

/**
 * Created by huazong on 2021/6/10.
 */

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.PopupMenu;
import android.support.v7.widget.RecyclerView;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;


public class MainChat extends AppCompatActivity {

    private static final String TAG = "MainChat";
    private List<ChatMsg> chatMsgList = new ArrayList<>();
    private RecyclerView msgRecyclerView;//RecyclerView是一个固定大小的View,但可以显示很多数据的控件。通过滚动列表来实现。
    private EditText inputText;
    private Button send;
    private LinearLayoutManager layoutManager;
    private MsgAdapter adapter;
    private ImageButton btn;
    private ImageView set;
    Intent intent;





    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mainchat);

        btn = (ImageButton)findViewById(R.id.btn);
        set = (ImageView) findViewById(R.id.set);
        msgRecyclerView =(RecyclerView ) findViewById(R.id.msg_recycler_view);
        inputText = (EditText) findViewById(R.id.input_text);
        send = (Button) findViewById(R.id.send);
        layoutManager = new LinearLayoutManager(this);
        adapter = new MsgAdapter(chatMsgList = getData());

        msgRecyclerView.setLayoutManager(layoutManager);
        msgRecyclerView.setAdapter(adapter);



        /**
         * 我们还需要为button建立一个监听器，我们需要将编辑框的内容发送到 RecyclerView 上：
            ①获取内容，将需要发送的消息添加到 List 当中去。
            ②调用适配器的notifyItemInserted方法，通知有新的数据加入了，赶紧将这个数据加到 RecyclerView 上面去。
            ③调用RecyclerView的scrollToPosition方法，以保证一定可以看的到最后发出的一条消息。*/
        send.setOnClickListener(new View.OnClickListener() {
            @Override

            public void onClick(View v) {
                String content = inputText.getText().toString();

                if(!content.equals("")) {
                    chatMsgList.add(new ChatMsg(content, ChatMsg.TYPE_SEND));
                    /*

                     */
                    adapter.notifyItemInserted(chatMsgList.size()-1);
                    msgRecyclerView.scrollToPosition(chatMsgList.size()-1);
                    inputText.setText("");//清空输入框中的内容
                }
            }
        });
/*设置按钮*/
        set.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showPopupMenu(set);
            }
        });
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mIntent();
            }
        });

    }


    /**/
    private List<ChatMsg> getData(){
        List<ChatMsg> list = new ArrayList<>();
        list.add(new ChatMsg("hello！", ChatMsg.TYPE_RECEIVED));
        return list;
    }
    /*返回按钮界面跳转*/
    public void mIntent(){
       intent = new Intent(MainChat.this,ThirdActivity.class);
        startActivity(intent);

    }
    /*菜单*/
    private void showPopupMenu(final View view) {
        final PopupMenu popupMenu = new PopupMenu(this,view);
        //menu 布局
        popupMenu.getMenuInflater().inflate(R.menu.menuchat,popupMenu.getMenu());
        //点击事件
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {

                    case R.id.clear:
                        clear();
                        break;
                    default:
                        break;
                }
                return false;
            }
        });
        //关闭事件
        popupMenu.setOnDismissListener(new PopupMenu.OnDismissListener() {
            @Override
            public void onDismiss(PopupMenu menu) {

            }
        });
        //显示菜单
        popupMenu.show();
    }



    private void clear(){    //清空

        AlertDialog.Builder builder  = new AlertDialog.Builder(MainChat.this);//确认框
        builder.setTitle("清空" ) ;
        builder.setMessage("是否确认清空？" ) ;
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                adapter.clearing();
            }
        });
        builder.setNegativeButton("取消", null);
        builder.show();



    }

    /*关闭系统软键盘 */

    public void closeInputMethod(){
        try {
            ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE))

                    .hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),

                            InputMethodManager.HIDE_NOT_ALWAYS);

        } catch (Exception e) { }finally{ }

    }






}

